#include "bsp_dac.h"


DAC_HandleTypeDef hdac1;

void bsp_dac1_init(void)
{
	DAC_ChannelConfTypeDef sConfig = {0};

	/** DAC Initialization
	*/
	hdac1.Instance = DAC1;
	HAL_DAC_Init(&hdac1);

	/** DAC channel OUT1 config
	*/
	sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
	sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
	sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
	sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
	sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
	HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1);
}

void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	if(hdac->Instance == DAC1)
	{
		/* Peripheral clock enable */
		__HAL_RCC_DAC1_CLK_ENABLE();

		__HAL_RCC_GPIOA_CLK_ENABLE();
		/**DAC1 GPIO Configuration
		PA4     ------> DAC1_OUT1
		*/
		GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
		GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
		GPIO_InitStruct.Pull = GPIO_NOPULL;
		HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

		/* DAC1 interrupt Init */
		HAL_NVIC_SetPriority(TIM6_DAC_LPTIM1_IRQn, 0, 0);
		HAL_NVIC_EnableIRQ(TIM6_DAC_LPTIM1_IRQn);
	}
}

void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
{
	if(hdac->Instance == DAC1)
	{
		/* clock disable */
		__HAL_RCC_DAC1_CLK_DISABLE();

		/**DAC1 GPIO Configuration
		PA4     ------> DAC1_OUT1
		*/
		HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4|GPIO_PIN_5);

		/* DAC1 interrupt DeInit */
		HAL_NVIC_DisableIRQ(TIM6_DAC_LPTIM1_IRQn);
	}
}

void TIM6_DAC_LPTIM1_IRQHandler(void)
{
	HAL_DAC_IRQHandler(&hdac1);
}

void bsp_dac_set_initial_voltage(void)
{
	/* Set DAC Channel data register: channel corresponding to ADC channel ADC_CHANNEL_4 */
	/* Set DAC output to 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V */
	HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, DIGITAL_SCALE_12BITS/2);

	/* Enable DAC Channel: channel corresponding to ADC channel ADC_CHANNEL_4 */
	HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);

	/* Set DAC Channel data register: channel corresponding to ADC channel ADC_CHANNEL_4 */
	/* Set DAC output to 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V */
	HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, DIGITAL_SCALE_12BITS/2);

	/* Enable DAC Channel: channel corresponding to ADC channel ADC_CHANNEL_4 */
	HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);
}

void bsp_dac_set_voltage(void)
{
	static uint8_t ub_dac_steps_count = 0;

	/* Set DAC voltage on channel corresponding to ADC_CHANNEL_4              */
	/* in function of user button clicks count.                                   */
	/* Set DAC output on 5 voltage levels, successively to:                       */
	/*  - minimum of full range (0 <=> ground 0V)                                 */
	/*  - 1/4 of full range (4095 <=> Vdda=3.3V): 1023 <=> 0.825V                 */
	/*  - 1/2 of full range (4095 <=> Vdda=3.3V): 2048 <=> 1.65V                  */
	/*  - 3/4 of full range (4095 <=> Vdda=3.3V): 3071 <=> 2.475V                 */
	/*  - maximum of full range (4095 <=> Vdda=3.3V)                              */
	HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ((DIGITAL_SCALE_12BITS * ub_dac_steps_count) / 4));

	/* Wait for voltage settling time */
	HAL_Delay(1);

	/* Manage ub_dac_steps_count to increment it in 4 steps and circularly.   */
	if (ub_dac_steps_count < 4)
	{
		ub_dac_steps_count++;
	}
	else
	{
		ub_dac_steps_count = 0;
	}
}

